const app = getApp();
const shareUtil = require('../../../utils/shareUtil.js');
var imgUrl=app.globalData.imgUrl

Page({
    data: {
        canvasInfo: {
            id: "mycanvas",
            width: 320,
            height: 568
        },
        showInfo: {
            width: 320,
            height: 568
        },
        systemInfo: {
            width: 320,
            height: 568
        },
        img: "",
        isChoose: false,
        canvasFlag: false
    },
    onLoad: function(e) {
        var that = this;
        wx.getSystemInfo({
            success: function(e) {
              that.setData({
                    "systemInfo.width": e.windowWidth,
                    "systemInfo.height": e.windowHeight
                });
            }
        });
    },
    imageToCanvas: function(e) {
        var canvas = this.data.canvasInfo,
        a = wx.createCanvasContext(canvas.id, this);
        a.drawImage(e, 0, 0, canvas.width, canvas.height),
        a.draw();
    },
    cutImage: function(t) {
        var that = this;
        wx.getImageInfo({
            src: t,
            success: function(a) {
                var i = a.width, n = a.height;
                wx.getSystemInfo({
                    success: function(a) {
                        that.setData({
                            "systemInfo.width": a.windowWidth,
                            "systemInfo.height": a.windowHeight,
                            "canvasInfo.width": i,
                            "canvasInfo.height": n,
                            "showInfo.width": a.windowWidth,
                            "showInfo.height": a.windowWidth / i * n
                        }), 
                        that.imageToCanvas(t);
                    }
                });
            }
        });
    },
    previewImage: function(t) {
        var e = this.data.canvasInfo,
        a = t.currentTarget.dataset.index;
        console.log(a),
        wx.canvasToTempFilePath({
          canvasId: e.id,
          x: a % 3 * e.width / 3,
          y: parseInt(a / 3) * e.height / 3,
          width: e.width / 3,
          height: e.height / 3,
          success: function(t) {
              console.log("成功路径： " + t.tempFilePath);
              var e = [];
              e.push(t.tempFilePath), 
              wx.previewImage({
                  urls: e,
                  current: t.tempFilePath,
                  success: function(t) {},
                  fail: function(t) {
                      console.log(t);
                  }
              });
          }
      }, this);
    },
    saveImage: function() {
        var t;
        if (1 != app.getJur()) {
            for (t = 0; t < 9; t++) 
            this.downImage(t);
            wx.showToast({
                title: "保存成功",
                icon: "none"
            });
        }
    },
    downImage: function(t) {
        var e = this.data.canvasInfo;
        console.log(t), 
        wx.canvasToTempFilePath({
            canvasId: e.id,
            x: t % 3 * e.width / 3,
            y: parseInt(t / 3) * e.height / 3,
            width: e.width / 3,
            height: e.height / 3,
            success: function(e) {
              console.log("成功路径： " + e.tempFilePath),
               wx.saveImageToPhotosAlbum({
                  filePath: e.tempFilePath,
                  success: function(e) {
                      console.log(t);
                  },
                  fail: function(t) {
                      console.log(t);
                  }
              });
            }
        }, this);
    },
    chooseImage: function(t) {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            success: function(t) {
              that.setData({
                img: t.tempFilePaths[0],
                isChoose: true,
                canvasFlag: true
              }),
              that.cutImage(t.tempFilePaths[0]),
              that.setData({
                canvasFlag: true
              })
            }
        });
    },
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});